################################################################
# Fulguro
# Copyright (C) 2004 Christophe Clienti
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
################################################################
include ../../Makefile.include

MODULE       = flgrmorpho

CC           = gcc
CFLAGS       = -fPIC -g -I../../include $(COMPILER_OPTIM)
LDFLAGS      = -L../../lib -l$(LIBNAME_BASE)

SWIG         = swig
SWIG_OPTIONS = $(SWIG_FLAGS) -I../../include

ifndef ($(SWIG_LANG))
	SWIG_LANG=python
endif


ifeq ($(SWIG_LANG),python)
	CFLAGS += -I$(PYTHON_HEADER)
	LDFLAGS += $(PYTHON_LIB)
	TO_INSTALL = _$(MODULE).so $(MODULE).py
	LIBNAME = _$(MODULE).so
	DOC_BIN = pydoc -w $(MODULE) && mv $(MODULE).html $(MODULE)
else
	SWIG_LANG = ruby
	TO_INSTALL = $(MODULE).so
	LIBNAME = $(MODULE).so
	CFLAGS += -I$(RUBY_HEADER)
	DOC_BIN = cat $(MODULE)_wrap.c | sed 's/rb_define_module_function/rb_define_method/' | sed 's/mFlgr/Flgr/' | sed 's/SWIGINTERN VALUE/static VALUE/' > doc_$(MODULE).c && rdoc doc_$(MODULE).c && mv doc/* $(MODULE) && rm -rf doc doc_$(MODULE).c
endif



.PHONY: doc

all: $(LIBNAME)

doc: $(LIBNAME)
	rm -rf doc $(MODULE) && mkdir $(MODULE)
	$(DOC_BIN)
	rm -rf ../../doc/wrapper/$(SWIG_LANG)/$(MODULE) && mkdir -p ../../doc/wrapper/$(SWIG_LANG)
	mv $(MODULE) ../../doc/wrapper/$(SWIG_LANG)

$(LIBNAME): $(MODULE)_wrap.o
	$(CC) $(LDFLAGS) -shared *.o -o $(LIBNAME)
	mkdir -p ../../lib/$(SWIG_LANG)Fulguro/fulguro
	cp $(TO_INSTALL) ../../lib/$(SWIG_LANG)Fulguro/fulguro

$(MODULE)_wrap.o: $(MODULE)_wrap.c

$(MODULE)_wrap.c: $(MODULE).i
	@echo -e "\n-> Wrapping" $(MODULE) "to" $(SWIG_LANG) "...\n"
	$(SWIG) $(SWIG_OPTIONS) -$(SWIG_LANG) $(MODULE).i

clean:
	@echo -e "\n-> Deleting $(SWIG_LANG) temp files ...\n"
	@rm -rf *~ *# *.so *.o *_wrap.c $(MODULE).py *.pyc *.png *.pgm
	@rm -rf doc $(MODULE)
